#include "tea_user.h"
#include "ui_tea_user.h"

tea_user::tea_user(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::tea_user)
{
    ui->setupUi(this);

    QTimer *timer =new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));
    timer->start(1000);//计时更新

    QDate DFD(2021,3,1);//规定学期第一天
    QDate CD = QDate::currentDate();
    int diff = (DFD.daysTo(CD))/7+1; //当前周

    ui->CurrentWeekNumber->setNum(diff);
    extern QString name;
    ui->name->setText(name);
//数据库连接准备
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setDatabaseName("experimentdb");
    db.setUserName("root");
    db.setPassword("1234");
    if(db.open()){
        qDebug("tea_SQL connect success!");
    }else{
        qDebug("tea_SQL connect failed!");
    }
//初始化实验班级下拉菜单，与数据库同步
    cmd = QString("select class_name from class_message");
    QSqlQuery query(cmd);
    while (query.next()) {
        QString list_buf = QString("%1").arg(query.value("class_name").toString());
        ui->Exp_Class->addItem(list_buf);
    }
//初始化实验课程下拉菜单，与数据库同步
    cmd = QString("select course_name from course_message");
    QSqlQuery query2(cmd);
    while (query2.next()) {
        QString list_buf = QString("%1").arg(query2.value("course_name").toString());
        ui->Exp_Course->addItem(list_buf);
    }
//初始化可预约的星期
    cmd = QString("select distinct exp_week from exp_time");
    QSqlQuery query3(cmd);
    while (query3.next()) {
        QString list_buf = QString("%1").arg(query3.value("exp_week").toString());
        ui->Week->addItem(list_buf);
    }
//预约实验数据源设定
    tea_model = new QSqlTableModel(this);
    tea_model->setTable("exp_reservation");
    tea_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
}

tea_user::~tea_user()
{
    delete ui;
}
void tea_user::timeUpdate(void)
{
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
    ui->CurrentDateTime->setText(str);
}
//查询实验时间
/*
  "第1大节(8:20-10:00)";
  "第2大节(10:20-12:00)";
  "第3大节(14:00-15:40)";
  "第4大节(16:00-17:40)";
  "第5大节(18:30-20:10)";
*/
void tea_user::on_Search_Time_clicked()
{
    uiWeek = ui->Week->currentText();
    QString buf[5];//note:如果将其放在private里，当变换查询条件时，该数组内的内容依旧不变，无法满足要求
    cmd = QString("select exp_time from exp_time where exp_week='%1'").arg(uiWeek);
    QSqlQuery query(cmd);
    int i = 0;
    while (query.next()) {
        buf[i] = QString("%1").arg(query.value("exp_time").toString());
        i++;
    }
    qDebug()<<buf[0];
    qDebug()<<buf[4];
    a = true;
    b = true;
    c = true;
    d = true;
    e = true;
    for (int j=0;j<i;j++) {
        if(buf[j] == "第1大节(8:20-10:00)"){
            a=false;
        }else if(buf[j] == "第2大节(10:20-12:00)"){
            b=false;
        }else if(buf[j] == "第3大节(14:00-15:40)"){
            c=false;
        }else if(buf[j] == "第4大节(16:00-17:40)"){
            d=false;
        }else if(buf[j] == "第5大节(18:30-20:10)"){
            e=false;
        }else{
            qDebug("over!");
        }
    }
    ui->checkBox_A->setDisabled(a);
    ui->checkBox_B->setDisabled(b);
    ui->checkBox_C->setDisabled(c);
    ui->checkBox_D->setDisabled(d);
    ui->checkBox_E->setDisabled(e);
}
//查询空闲实验时间
void tea_user::on_Search_FreeTime_clicked()
{
    uiWeek = ui->Week->currentText();
    uiWeekNumber = ui->WeekNumber->currentText();
    qDebug()<<a<<b<<c<<d<<e;
    QString buf[5];
    cmd = QString("select 时间段 from exp_reservation where 周数 ='%1' and 星期 ='%2'").arg(uiWeekNumber).arg(uiWeek);
    QSqlQuery query(cmd);
    int i = 0;
    while (query.next()) {
        buf[i] = QString("%1").arg(query.value("时间段").toString());
        i++;
    }
    qDebug()<<buf[0];
    qDebug()<<buf[4];
    for (int j=0;j<i;j++) {
        if(buf[j] == "第1大节(8:20-10:00)"){
            a=true;
        }else if(buf[j] == "第2大节(10:20-12:00)"){
            b=true;
        }else if(buf[j] == "第3大节(14:00-15:40)"){
            c=true;
        }else if(buf[j] == "第4大节(16:00-17:40)"){
            d=true;
        }else if(buf[j] == "第5大节(18:30-20:10)"){
            e=true;
        }else{
            qDebug("over!");
        }
    }
    ui->checkBox_A->setDisabled(a);
    ui->checkBox_B->setDisabled(b);
    ui->checkBox_C->setDisabled(c);
    ui->checkBox_D->setDisabled(d);
    ui->checkBox_E->setDisabled(e);
}
//提交预约
void tea_user::on_ApplyBt_clicked()
{
    uiWeekNumber = ui->WeekNumber->currentText();
    uiWeek = ui->Week->currentText();
    if(ui->checkBox_A->isChecked()){
        uiTime = "第1大节(8:20-10:00)";
    }else if(ui->checkBox_B->isChecked()){
        uiTime = "第2大节(10:20-12:00)";
    }else if(ui->checkBox_C->isChecked()){
        uiTime = "第3大节(14:00-15:40)";
    }else if(ui->checkBox_D->isChecked()){
        uiTime = "第4大节(16:00-17:40)";
    }else if(ui->checkBox_E->isChecked()){
        uiTime = "第5大节(18:30-20:10)";
    }else{
        uiTime = nullptr;
    }
    uiExpCourse = ui->Exp_Course->currentText();
    uiExpName = ui->Exp_Name->text();
    uiExpClass = ui->Exp_Class->currentText();
    extern QString name;
    qDebug()<<name;
    cmd = QString("select * from class_message where class_name ='%1'").arg(uiExpClass);
    QSqlQuery query(cmd);
    while (query.next()) {
        ExpMembers = query.value(1).toInt();
    }
    qDebug()<<ExpMembers;
    QDate DFD(2021,3,1);//规定学期第一天
    QDate CD = QDate::currentDate();
    int diff = (DFD.daysTo(CD))/7+1; //当前周
    qDebug()<<diff;
    if(uiExpName == nullptr){
        QMessageBox::warning(this,"提示","请输入实验项目(名称)！");
    }else if(uiTime == nullptr){
        QMessageBox::warning(this,"提示","请选择合适的时间段");
    }else if(diff>=uiWeekNumber.toInt()){
        QMessageBox::critical(this,"警告","您只能预约下周及以后的实验");
    }else{
        cmd = QString("insert into exp_reservation(周数,星期,时间段,实验课程,实验项目,预约者,实验班级,班级人数) values('%1', '%2','%3','%4','%5','%6','%7','%8')").arg(uiWeekNumber).arg(uiWeek).arg(uiTime).arg(uiExpCourse).arg(uiExpName).arg(name).arg(uiExpClass).arg(ExpMembers);
        QSqlQuery query2(cmd);
        QMessageBox::information(this,"恭喜","预约成功！");
    }
}
//查询自己的预约情况
void tea_user::on_Search_clicked()
{
    ui->ApplyView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    extern QString name;
    cmd = QString("预约者 ='%1'").arg(name);
    tea_model->setFilter(cmd);
    tea_model->select();
    tea_model->setHeaderData(0,Qt::Horizontal,tr("周数"));
    tea_model->setHeaderData(1,Qt::Horizontal,tr("星期"));
    tea_model->setHeaderData(2,Qt::Horizontal,tr("时间段"));
    tea_model->setHeaderData(3,Qt::Horizontal,tr("实验课程"));
    tea_model->setHeaderData(4,Qt::Horizontal,tr("实验项目"));
    tea_model->setHeaderData(5,Qt::Horizontal,tr("预约者"));
    tea_model->setHeaderData(6,Qt::Horizontal,tr("实验班级"));
    tea_model->setHeaderData(7,Qt::Horizontal,tr("班级人数"));
    ui->ApplyView->setModel(tea_model);
}
//删除自己的预约情况
void tea_user::on_Delete_clicked()
{
    select = ui->ApplyView->selectionModel();
    QModelIndexList list =select->selectedRows();
    for(int i=0;i<list.size();i++){//可删除一行或多行数据
        tea_model->removeRow(list.at(i).row());
    }
}
//提交当前的操作
void tea_user::on_Save_clicked()
{
    tea_model->submitAll();
    if(tea_model->submitAll()){
        QMessageBox::information(this,"提示","操作成功！");
    }else{
        QMessageBox::critical(this,"提示","操作失败！");
    }
}
//撤销当前的操作
void tea_user::on_Cancel_clicked()
{
    tea_model->revertAll();
    tea_model->submitAll();
}
